package com.app.aipass.viewmodel;

import android.app.Application;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.app.aipass.app.AppApplication;
import com.app.aipass.data.DemoRepository;
import com.app.aipass.entity.FindPswEntity;
import com.app.aipass.entity.UserEntity;
import com.app.aipass.http.response.ApiException;
import com.app.aipass.utils.ToastUtil;
import com.app.aipass.utils.Utils;

import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.ResponseBody;

public class LoginViewModel extends BaseViewModel<DemoRepository> {

    public SingleLiveEvent<FindPswEntity> mFindPswEntity = new SingleLiveEvent<>();

    public SingleLiveEvent<FindPswEntity> mLoginEntity = new SingleLiveEvent<>();

    public LoginViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }

    public void smsCode(String phone,TextView send_yzm_tv)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("type","1");
        map.put("phone",phone);//挂单数量
        model.smsCode(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<ResponseBody>() {
                    @Override
                    public void onNext(ResponseBody response) {
                        Log.e("TAG","============smsCode=====onNext========");
                        if(null != response)
                        {
//                            mPlanIndexEntity.setValue(response.getData());
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","============smsCode=====onError========");
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShortSafe(ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody));
                        }
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","============smsCode=====onComplete========");
                        ToastUtils.showShortSafe("发送成功");
                        send_yzm_tv.setEnabled(false);
//                        send_yzm_tv.setBackgroundResource(R.drawable.shape_d9dade_50);
                        Utils.showCount(send_yzm_tv);
                    }
                });
    }

    public void findPassword(String mobile,String code,String password)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code);
        map.put("password",password);
        model.findPassword(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<FindPswEntity>() {
                    @Override
                    public void onNext(FindPswEntity findPswEntity) {
                        mFindPswEntity.postValue(findPswEntity);
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShortSafe(ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody));
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }

    public void aiLogin(String mobile,String code,String password)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code);
        map.put("password",password);
        model.aiLogin(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<FindPswEntity>() {
                    @Override
                    public void onNext(FindPswEntity findPswEntity) {
                        mLoginEntity.postValue(findPswEntity);
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShortSafe(ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody));
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }

}
